package com.demo.tx.seata.service.tcc;

import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class SeataTccExecuteService {

    @Resource
    private SeataTCCService seataTCCService;
    @Resource
    private SeataTCCRemoteService seataTCCRemoteService;

    @GlobalTransactional(rollbackFor = Exception.class)
    public void execute(Long userId, Long sleepTime) throws InterruptedException {
        seataTCCService.prepare(null, userId, sleepTime);
    }

    /**
     * 测试TCC模式事务，远程调用时，其中一个服务出现超时，则整个事务会回滚
     * @param userId 用户id
     * @param sleepTime 睡眠时间
     * @throws InterruptedException 异常
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public void executeRemote(Long userId, Long sleepTime) throws InterruptedException {
        seataTCCRemoteService.prepare(null, userId, sleepTime);
    }
}
